<template>
  <div class="employees">
    <el-card>
      <div style="text-align: right">
        <el-button @click="exportExcel">导出</el-button>
        <el-button
          type="primary"
          @click="$router.push('/employees/import')"
        >导入</el-button>
        <el-button type="primary" @click="openDialog">+新增员工</el-button>
      </div>
    </el-card>
    <el-card style="margin: 20px 0">
      <el-table :data="list" border>
        <el-table-column label="序号" type="index">
          <template v-slot="{ $index }">
            <div>
              {{ (page.page - 1) * page.size + $index + 1 }}
            </div>
          </template>
        </el-table-column>
        <el-table-column sortable label="姓名" prop="username" />
        <el-table-column sortable label="手机号" prop="mobile" />
        <el-table-column sortable label="头像" prop="staffPhoto">
          <template v-slot="{ row }">
            <img
              v-globalImageError="defaultImg"
              width="50px"
              :src="row.staffPhoto"
              alt=""
              @click="openImg(row.staffPhoto)"
            >
          </template>
        </el-table-column>
        <el-table-column sortable label="工号" prop="workNumber" />
        <el-table-column sortable label="聘用形式" prop="formOfEmployment" />
        <el-table-column sortable label="部门" prop="departmentName" />
        <el-table-column sortable label="入职时间" prop="timeOfEntry" />
        <el-table-column sortable label="状态" prop="enableState" />
        <el-table-column label="操作" width="300px" fixed="right">
          <template v-slot="{ row }">
            <div>
              <el-button
                type="text"
                @click="$router.push('/employees/details/' + row.id)"
              >查看</el-button>
              <el-button type="text">转正</el-button>
              <el-button type="text">调岗</el-button>
              <el-button type="text">离职</el-button>
              <el-button type="text" @click="setRole(row.id)">角色</el-button>
              <el-button type="text" @click="delEmp(row.id)">删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div style="text-align: right; margin-top: 20px">
        <el-pagination
          layout="total,sizes,prev,pager,next,jumper"
          :total="page.total"
          :current-page="page.page"
          :page-size="page.size"
          :page-sizes="[1, 5, 10, 15, 20, 100]"
          @current-change="pageChange"
          @size-change="sizeChange"
        />
      </div>
    </el-card>
    <Add ref="add" />
    <!-- el-dialog特性,首次激活dom没有完全加载,所以里面代码需要等dom渲染完成才执行 -->
    <el-dialog title="头像二维码显示" :visible.sync="isShow">
      <div style="text-align: center">
        <canvas ref="canvas" />
      </div>
    </el-dialog>
    <Role ref="role" />
  </div>
</template>

<script>
import { sysUser, sysUserDelete } from '@/api/employees'
import employeesData from '@/api/constant/employees'
import Add from './components/Add.vue'
import Role from './components/Role.vue'
import moment from 'moment'
import cookieJs from 'js-cookie'
import Qrcode from 'qrcode'
export default {
  components: {
    Add,
    Role
  },
  data() {
    return {
      defaultImg: require('@/assets/common/bigUserHeader.png'),
      list: [],
      employeesData,
      page: {
        page: 1,
        size: +cookieJs.get('size') || 5,
        total: 20
      },
      isShow: false
    }
  },
  created() {
    this.getData()
  },
  methods: {
    // 分配角色
    setRole(id) {
      this.$refs.role.show = true
      this.$refs.role.getRoleById(id)
    },
    // 打开头像二维码
    openImg(imgStr) {
      this.isShow = true
      if (!imgStr) return
      this.$nextTick(() => {
        Qrcode.toCanvas(this.$refs.canvas, imgStr, {
          width: 300,
          height: 300
        })
      })
    },
    // 打开新增员工对话框
    openDialog() {
      this.$refs.add.isShow = true
    },
    pageChange(page) {
      this.page.page = page
      this.getData()
    },
    sizeChange(size) {
      this.page.size = size
      this.page.page = 1
      cookieJs.set('size', size)
      this.getData()
    },
    // 获取用户信息
    async getData() {
      const res = await sysUser(this.page)
      // console.log(res)
      res.data.rows.forEach((item) => {
        const result = this.employeesData.hireType.find((item2) => {
          return +item.formOfEmployment === item2.id
        })
        const result2 = this.employeesData.workingState.find((item3) => {
          return '' + item.enableState === item3.id
        })
        item.formOfEmployment = result ? result.value : '-'
        item.enableState = result2 ? result2.value : '-'
        item.timeOfEntry = moment(item.timeOfEntry).format('YYYY-MM-DD')
      })
      this.page.total = res.data.total
      this.list = res.data.rows
    },
    // 删除员工
    delEmp(id) {
      this.$confirm('您确定要删除该员工吗?', '温馨提示').then(async() => {
        await sysUserDelete(id)
        this.$message.success('删除成功!')
        this.pageChange(1)
        this.getData()
      })
    },
    // 导出员工列表
    async exportExcel() {
      const header = {
        姓名: 'username', // // string	非必须		姓名
        手机号: 'mobile', // // string	非必须		手机号
        入职时间: 'timeOfEntry', // // string	非必须		入职时间
        聘用形式: 'formOfEmployment', // // number	非必须		聘用形式
        工号: 'workNumber', // // string	非必须		工号
        组织名称: 'departmentName', // // string	非必须		组织名称
        转正时间: 'correctionTime' // // string	非必须		转正时间
      }
      const res = await sysUser({
        page: 1,
        size: 10000
      })
      // console.log(res)
      // 复杂数据类型,使用该数据的地方只要有一个地方(堆)改了,使用该引用的地方都会变
      res.data.rows.forEach((item) => {
        const result = this.employeesData.hireType.find((item2) => {
          return +item.formOfEmployment === item2.id
        })
        const result2 = this.employeesData.workingState.find((item3) => {
          return '' + item.enableState === item3.id
        })
        item.formOfEmployment = result ? result.value : '-'
        item.enableState = result2 ? result2.value : '-'
        item.timeOfEntry = moment(item.timeOfEntry).format('YYYY-MM-DD')
      })
      this.page.total = res.data.total
      this.list = res.data.rows
      // data数据,需求是将list数据数组中每一项(对象)中的值按照header的次序依次取出
      // 循环整个列表数据,为了产生一个只有值的二维数据[[]],它的数据次序要按照头部数组次序来
      const data = this.list.map((item) => {
        // 循环头部数据
        return Object.keys(header).map((item2) => {
          // 按照次序return相应值,最终产生按次序的新数组
          return item[header[item2]]
        })
      })
      // 使用参数值
      import('@/api/vendor/Export2Excel.js').then((result) => {
        // console.log(res)
        result.export_json_to_excel({
          header: Object.keys(header),
          data,
          filename: '员工列表'
        })
      })
    }
  }
}
</script>

<style style="scss" scoped>
.employees {
  padding: 15px;
}
</style>
